﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;
using 纺织贸易管理系统.选择窗体;

namespace 后整理管理系统.出库管理
{
    public partial class 销售客诉单列表 : Form
    {
        public 销售客诉单列表()
        {
            InitializeComponent();
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = dateEdit2.DateTime.AddDays(-QueryTime.间隔);
            CreateGrid.Create(this.Name, gridView1);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                MainForm.mainform.AddMidForm(new 销售客诉单(new Model.CustomerComplaints(), Model.FormUseful.新增));
            }
            catch (Exception ex)
            {
                Tools.ShowMessageBox.ShowErrorBox(ex.Message);
            }
        }
        #region 弹出菜单
        private void 保存样式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CreateGrid.SaveGridview(this.Name, gridView1);
        }
        #endregion

        private void 配置列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var fm = new 配置列(gridView1) { formname = this.Name, Obj = null, dt = gridControl1.DataSource as DataTable };
            fm.ShowDialog();
        }

        private void 导出到EXCELToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "销售客诉报表");
        }

        private void 生产客诉报表_Load(object sender, EventArgs e)
        {
            Query();
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (AccessBLL.CheckAccess("修改销售客诉单") == true)
            {
                var doc = Connect.dbcontext.Queryable<CustomerComplaints>().First (x => x.DocNum == gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString());
                if (doc != null)
                {
                    MainForm.mainform.AddMidForm(new 销售客诉单(doc, Model.FormUseful.修改));
                }
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var docnum = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "DocNum").ToString();
                var doc = Connect.dbcontext.Queryable<CustomerComplaints>().First (x => x.DocNum == docnum);
                if (doc!=null)
                {             
                    Connect.dbcontext.BeginTran();
                    销售客诉BLL.反审核(docnum);
                    Connect.dbcontext.Deleteable<CustomerComplaints>(x => x.DocNum == docnum).ExecuteCommand ();
                    Connect.dbcontext.Deleteable<CustomerComplaintsDetail>(x => x.DocNum == docnum).ExecuteCommand();
                    Connect.dbcontext.CommitTran();
                }
            }
            catch (Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                Tools.ShowMessageBox.ShowErrorBox("删除的时候发生了错误:" + ex.Message);
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            gridControl1.DataSource = Connect.dbcontext.Ado.GetDataTable ($"select * from CustomerComplaints c left join danjumingxitable d on c.DocNum=d.danhao where c.SupplierName like '%{txtksmc.Text}%' and c.DocDate between '{dateEdit1.DateTime.Date}' and '{dateEdit2.DateTime.Date.AddDays(1)}' ");
        }

      

        private void txtksmc_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var fm = new 客户选择() { linkman = new LXR() { MC = "", ZJC = "" } };
            fm.ShowDialog();
            txtksmc.Text = fm.linkman.MC;
            Query();
        }
    }
}
